package com.shuaiche.yeguan.ui.upload;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.shuaiche.yeguan.R;
import com.shuaiche.yeguan.util.DensityUtils;
import com.shuaiche.yeguan.util.DeviceHelper;
import com.shuaiche.yeguan.util.GlideUtil;

import java.util.Collections;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class CarPictureAdapter extends RecyclerView.Adapter<CarPictureAdapter.ViewHolder> {
    public static final String ADD_PIC = "add";
    private Context context;
    private List<String> pictureList;
    private OnItemClickListener listener;

    public CarPictureAdapter(Context context,List<String> pictureList) {
        this.context = context;
        this.pictureList = pictureList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_car_picture, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String picturePath = pictureList.get(position);
        ViewGroup.LayoutParams params = holder.ivCarPicture.getLayoutParams();
        int picWidth = DeviceHelper.getScreenWidthPx(context)/4;
        params.height = picWidth-20;
        holder.ivCarPicture.setLayoutParams(params);

        if (ADD_PIC.equals(picturePath)){
            holder.btnDelete.setVisibility(View.GONE);
            holder.ivCarPicture.setImageResource(R.mipmap.addpic);
        }else {
            GlideUtil.loadImg(context,picturePath,holder.ivCarPicture);
            holder.btnDelete.setVisibility(View.VISIBLE);
            holder.btnDelete.setOnClickListener(v -> {
                if (listener != null) {
                    listener.onDeleteClick(position);
                }
            });
        }

        holder.ivCarPicture.setOnClickListener(v -> {
            if (listener != null){
                listener.onItemClick(position);
            }
        });


    }

    @Override
    public int getItemCount() {
        return pictureList.size();
    }

    public void swapItems(int fromPosition, int toPosition) {
        Collections.swap(pictureList, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    public void removeItem(int position) {
        pictureList.remove(position);
        notifyDataSetChanged();
//        notifyItemRemoved(position);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    public interface OnItemClickListener {
        void onDeleteClick(int position);
        void onItemClick(int position);
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivCarPicture;
        ImageButton btnDelete;

        ViewHolder(View itemView) {
            super(itemView);
            ivCarPicture = itemView.findViewById(R.id.iv_car_picture);
            btnDelete = itemView.findViewById(R.id.btn_delete);
        }
    }
}